package com.example.tellwin.mine.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseFragment;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.home.act.QuestionDetailActivity;
import com.example.tellwin.home.adpater.AttentionQuestionAdapter;
import com.example.tellwin.mine.bean.AttentionQuestionBean;
import com.example.tellwin.mine.contract.AttentionQuestionContract;
import com.example.tellwin.mine.presenter.AttentionQuestionPresenter;
import com.example.tellwin.utils.CommonUtils;
import com.example.tellwin.view.ShareDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 *
 */
public class AttentionQuestionFragment extends CpBaseFragment implements AttentionQuestionContract.View {


    @BindView(R.id.attention_questoin_rv)
    RecyclerView attentionQuestoinRv;

    private Unbinder unbinder;
    private AttentionQuestionAdapter attentionQuestionAdapter;
    private List<AttentionQuestionBean> attentionQuestionBeans = new ArrayList<>();

    @Inject
    AttentionQuestionPresenter mPresenter;

    @Override
    public View onCreateRootView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_attention_question, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        initEvent();
        initData();
        return view;
    }

    private void initView(View view) {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        attentionQuestoinRv.setLayoutManager(linearLayoutManager);
        attentionQuestoinRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view);
                if (position == 0) {
                    outRect.top = getResources().getDimensionPixelOffset(R.dimen.dp_30);
                }
                outRect.bottom = getResources().getDimensionPixelOffset(R.dimen.dp_15);
            }
        });
        attentionQuestionAdapter = new AttentionQuestionAdapter();
        attentionQuestoinRv.setAdapter(attentionQuestionAdapter);
    }

    private void initEvent() {
        attentionQuestoinRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (CommonUtils.isSlideToBottom(attentionQuestoinRv)) {
                    mPresenter.getAttentionQuestionList(true);
                }
            }
        });
        attentionQuestionAdapter.setAttentionQuestionListener(new AttentionQuestionAdapter.AttentionQuestionListener() {
            @Override
            public void onItemLikeClick(int position, AttentionQuestionBean questionBean) {
                mPresenter.likeQuestion(questionBean.getQuestionId(), position);
            }

            @Override
            public void onItemShareClick(int position, AttentionQuestionBean questionBean) {
                ShareDialog shareDialog = new ShareDialog(getContext() , 2 ,questionBean.getQuestionId() ,questionBean.getQuestionName());
                shareDialog.show();
            }

            @Override
            public void onItemClick(int position, AttentionQuestionBean questionBean) {
                Intent intent = new Intent(getActivity(), QuestionDetailActivity.class);
                intent.putExtra(Common.QUESTION, questionBean.getQuestionId());
                startActivity(intent);
            }

            @Override
            public void onAttentionItemClick(int position, AttentionQuestionBean questionBean) {
                mPresenter.attention(position, questionBean.getQuestionId());
            }
        });
    }

    private void initData() {
        mPresenter.getAttentionQuestionList(false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void attentionQuestionListResult(boolean isLoadMore, List<AttentionQuestionBean> attentionQuestionBeans) {
        if (isLoadMore) {
            this.attentionQuestionBeans.addAll(attentionQuestionBeans);
            attentionQuestionAdapter.notifyDataSetChanged();
        } else {
            this.attentionQuestionBeans = attentionQuestionBeans;
            attentionQuestionAdapter.setAttentionQuestionBeans(this.attentionQuestionBeans);
        }
    }

    @Override
    public void likeQuestionResult(int position) {
        String like = attentionQuestionBeans.get(position).getIsLike();
        if ("0".equals(like)) {
            like = "1";
        } else {
            like = "0";
        }
        attentionQuestionBeans.get(position).setIsLike(like);
        attentionQuestionAdapter.notifyDataSetChanged();
    }

    @Override
    public void attention(int position, String questionId) {
        attentionQuestionBeans.remove(position);
        attentionQuestionAdapter.notifyDataSetChanged();
    }
}
